package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.User;
import com.itheima.reggie.mapper.UserMapper;
import com.itheima.reggie.service.UserService;
import com.itheima.reggie.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
 implements UserService {
 @Autowired
 private RedisTemplate redisTemplate;
 public R<String> sendMsg(HttpSession session, User user) {

  String phone = user.getPhone();
  //生成验证码
  if (StringUtils.hasText(phone)) {
   String code = ValidateCodeUtils.generateValidateCode4String(6).toString();
   log.info("瑞吉外卖验证码code为: {}", code);
   //调用阿里云短信服务api完成发送短信
//    sendsms.sendMessage(args,phone);
   //SMSUtils.sendMessage("瑞吉外卖",phone,validateCode4String);
   //保存验证码
//   session.setAttribute(phone, code);
 //把短信缓存到redis,并且设置过期时间为5分钟
   redisTemplate.opsForValue().set(phone,code,5, TimeUnit.MINUTES);
   return R.success("发送验证码成功");
  }
  return R.error("发送验证码失败");

 }

 @Override
 public R<User> login( Map map, HttpSession session) {
  //获取手机号
  String phone = map.get("phone").toString();
  //获取验证码
  String code = map.get("code").toString();
  //从redis中取出验证码，并且比较
  Object redisCode = redisTemplate.opsForValue().get(phone);
  if (redisCode!= null && redisCode.equals(code)) {
   LambdaQueryWrapper<User> queryWrapper = Wrappers.<User>lambdaQuery().eq(User::getPhone, phone);
   User user = this.getOne(queryWrapper);
   if (user == null) {
    user = new User();
    user.setPhone(phone);
    user.setStatus(1);
    this.save(user);
   }
   session.setAttribute("user", user.getId());
   //如果用户登陆成功，就删除缓存验证码
   redisTemplate.delete(phone);
   return R.success(user);
  }
  return R.error("登陆失败！");
  }
}


  //成功 登录 判断是否为新用户
  //新用户自动插入注册


  //获取手机号
  /*String phone = map.get("phone").toString();
  //获取验证码
  String code = map.get("code").toString();
  //与session中的验证码进行比对
  if (session.getAttribute(phone) != null && session.getAttribute(phone).equals(code)) {
   //成功 登录 判断是否为新用户
   LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
   queryWrapper.eq(User::getPhone, phone);
   User user = this.getOne(queryWrapper);
   if (user == null) {
    //新用户自动插入注册
    user = new User();
    user.setPhone(phone);
    user.setStatus(1);
    this.save(user);
   }
   session.setAttribute("user", user.getId());
   return R.success(user);
  }
  return R.error("登陆失败！");*/
